// pages/user-edit/user-edit.js
const API = require('../../utils/api.js'); // 引入封装好的 API 方法

Page({
  data: {
    userInfo: {},
    defaultAvatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
    avatarNew:'',
    phoneError: '',
    emailError: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    // 获取当前用户信息
    const app = getApp();
    this.setData({
      userInfo: app.globalData.userInfo
    });
  },
  // 选择头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    this.setData({
      'userInfo.avatar': avatarUrl,
      'avatarNew':avatarUrl
    });
  },
  // 输入昵称
  onNickNameInput(e) {
    this.setData({
      'userInfo.nickName': e.detail.value
    });
  },
  // 输入手机号 - 实时校验
  onPhoneInput(e) {
    const phone = e.detail.value;
    let phoneError = '';
    if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
      phoneError = '手机号格式不正确';
    }
    this.setData({
      'userInfo.phoneNo': phone,
      phoneError: phoneError
    });
  },
  // 输入邮箱 - 实时校验
  onEmailInput(e) {
    const email = e.detail.value;
    let emailError = '';
    if (email && !/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(email)) {
      emailError = '邮箱格式不正确';
    }
    this.setData({
      'userInfo.email': email,
      emailError: emailError
    });
  },
  // 输入个人简介
  onBioInput(e) {
    this.setData({
      'userInfo.remarks': e.detail.value
    });
  },
  // 导航返回
  navigateBack() {
    wx.navigateBack();
  },
  // 保存用户信息
  async saveUserInfo() {
    const app = getApp();
    const {phoneError, emailError } = this.data;
    // 基本验证
    if (!this.data.userInfo.nickName) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      });
      return;
    }
    // 检查是否有错误
    if (phoneError || emailError) {
      wx.showToast({
        title: '请修正输入的错误',
        icon: 'none'
      });
      return;
    }
    if(this.data.userInfo.phoneNo==null){
      this.setData({
        'userInfo.phoneNo':''
      })
    }
    if(this.data.avatarNew){
      API.updateUserInfo({
        name:'file',
        filePath: this.data.userInfo.avatar,
        formData: this.data.userInfo
      }).then(res=>{
        app.globalData.userInfo = res;
        wx.showToast({
          title: '保存成功',
          icon: 'none'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }).catch(err=>{
        console.log(err)
        wx.showToast({
          title: '保存失败，请重试',
          icon: 'none'
        });
      })
    }else{
      API.updateUserInfoNoAvatar(this.data.userInfo).then(res=>{
        app.globalData.userInfo = res;
        wx.showToast({
          title: '保存成功',
          icon: 'none'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }).catch(err=>{
        console.log(err)
        wx.showToast({
          title: '保存失败，请重试',
          icon: 'none'
        });
      })
    }
    
  }
});